package com.platform.service.shop.impl;

import com.platform.dao.shop.MlsUserDao;
import com.platform.dao.shop.UserDao;
import com.platform.dao.shop.UserRecordDao;
import com.platform.entity.MlsUserEntity2;
import com.platform.entity.shop.UserCouponEntity;
import com.platform.entity.shop.UserEntity;
import com.platform.entity.shop.UserRecordEntity;
import com.platform.service.shop.UserCouponService;
import com.platform.service.shop.UserService;
import com.platform.utils.CharUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-16 15:02:28
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private MlsUserDao mlsUserDao;

    @Autowired
    private UserRecordDao userRecordDao;

    @Autowired
    private UserCouponService userCouponService;

    @Override
    public UserEntity queryObject(Integer id) {
        UserEntity userEntity = userDao.queryObject(id);

        MlsUserEntity2 mlsUserEntity2 = mlsUserDao.queryByUserId(new Long(id));
        if(mlsUserEntity2.getFid() != null){
            MlsUserEntity2 fUser = mlsUserDao.queryObject(mlsUserEntity2.getFid());
            userEntity.setfId(fUser.getUserId().intValue());
            userEntity.setfName(fUser.getNickname());
        }
        return userEntity;
    }

    @Override
    public List<UserEntity> queryList(Map<String, Object> map) {
        return userDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return userDao.queryTotal(map);
    }

    @Override
    public int save(UserEntity user) {
        user.setRegisterTime(new Date());
        return userDao.save(user);
    }

    @Override
    public int update(UserEntity user) {

        int update = userDao.update(user);
        // 判断是否需要修改上级
        if(user.getfId() > 0){
            MlsUserEntity2 mlsUser = new MlsUserEntity2();
            mlsUser.setUserId(new Long(user.getId()));
            // 根据fid查找，这里的fid实际上是userId
            MlsUserEntity2 mlsUserEntity2 = mlsUserDao.queryByUserId(new Long(user.getfId()));
            mlsUser.setFid(mlsUserEntity2.getMlsUserId());
            mlsUserDao.updatefid(mlsUser);
        }

        return update;
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return userDao.deleteBatch(ids);
    }

    /**
     * 推广关系变更
     * @param user
     */
    @Override
    public void updatePromoter(UserEntity user) {
        userDao.updatePromoter(user);
    }

    @Override
    public void charge(UserEntity user) {

        MlsUserEntity2 mlsUserEntity2 = mlsUserDao.queryByUserId(new Long(user.getId()));

        UserRecordEntity userRecordEntity = new UserRecordEntity();
        userRecordEntity.setMlsUserId(mlsUserEntity2.getMlsUserId());
        userRecordEntity.setTypes(1);// 后台调整
        userRecordEntity.setTypesStr("后台调整");
        userRecordEntity.setPrice(user.getAmount());
        String rk = user.getIsCalFee().equals(0) ? "活动余额" : "可用余额";
        userRecordEntity.setRemarks("充值"+rk+"："+user.getAmount());
        if(user.getIsCalFee().equals(0) ){
            userRecordEntity.setAmount(new BigDecimal(mlsUserEntity2.getGetProfit()  + user.getAmount()));
        }else{
            userRecordEntity.setAmount(new BigDecimal(mlsUserEntity2.getAllProfit() + user.getAmount()));
        }
        userRecordEntity.setDirection(1);
        userRecordDao.save(userRecordEntity);

        if(user.getIsCalFee().equals(0)){// 不计提手续费，充值到活动余额
            mlsUserEntity2.setGetActivityProfit(user.getAmount());
            mlsUserEntity2.setGetProfit(0);
        }else{// 计提手续费，充值到可用余额
            mlsUserEntity2.setGetActivityProfit(0);
            mlsUserEntity2.setGetProfit(user.getAmount());
        }
        mlsUserEntity2.setGetPoints(0);// 不改变积分
        mlsUserDao.updateMoney(mlsUserEntity2);

        // 是否发送活动现金券
        if(user.getIsCoupon().equals(1) ){
            UserCouponEntity userCouponVo = new UserCouponEntity();
            userCouponVo.setCouponId(30);// 写死 30 ，活动现金券
            userCouponVo.setUserId(user.getId());
            userCouponVo.setAddTime(new Date());
            userCouponVo.setCouponPrice(100);
            userCouponVo.setMerchantId(100001L);
            userCouponVo.setCouponNumber(CharUtil.getRandomString(12));
            userCouponService.save(userCouponVo);
        }
    }

    @Override
    public void chargePoints(UserEntity user) {

        MlsUserEntity2 mlsUserEntity2 = mlsUserDao.queryByUserId(new Long(user.getId()));

        UserRecordEntity userRecordEntity = new UserRecordEntity();
        userRecordEntity.setMlsUserId(mlsUserEntity2.getMlsUserId());
        userRecordEntity.setTypes(1);// 后台调整
        userRecordEntity.setTypesStr("后台调整");
        userRecordEntity.setPrice(user.getPoints());
        userRecordEntity.setRemarks("充值积分:"+user.getPoints()+",总积分:"+(mlsUserEntity2.getPoints()  + user.getPoints()));
        // 可用余额不变
        userRecordEntity.setAmount(new BigDecimal(mlsUserEntity2.getAllProfit()));

        userRecordEntity.setDirection(1);
        userRecordDao.save(userRecordEntity);

        mlsUserEntity2.setGetPoints(user.getPoints());
        mlsUserEntity2.setGetProfit(0);// 不改变余额
        mlsUserEntity2.setGetActivityProfit(0);// 不改变活动余额
        mlsUserDao.updateMoney(mlsUserEntity2);
    }

}
